/**展示所有商品*/
function showCartList (){
    $("#product-list").empty();
    $.ajax({
        url: "/api/products/all_List",
        type: "GET",
        xhrFields: {
            withCredentials: true
        },
        dataType: "JSON",
        success: function(json) {
            let list = json.data;
            for (let i = 0; i < list.length; i++) {
                let tr = '<tr id=""  >\n' +
                    '<td><img src="/api#{image}collect.png" class="img-responsive" /></td>\n' +
                    '<td>#{title}</td>\n' +
                    '<td><span >#{createdUser}</span></td>\n' +
                    '<td>#{num}</td>\n' +
                    '<td>￥<span >#{price}</span></td>\n' +
                    '<td>#{createTime}</td>\n' +
                    '<td>\n' +
                    '<input type="button" onclick="deleteProduct(#{id})" class="cart-del btn btn-default btn-xs" value="删除" />\n' +
                    '</td>\n' +
                    '</tr>'

                //当参数很多的时候：1、replaceAll("#{cid}", list[i].cid);要么replace(/#{cid}/g, list[i].cid);
                //   /xxx/g 为标准结构
                tr = tr.replace(/#{image}/g, list[i].image);
                tr = tr.replace("#{title}", list[i].title);
                tr = tr.replace("#{createdUser}", list[i].createdUser);
                tr = tr.replace(/#{num}/g, list[i].num);
                tr = tr.replace(/#{price}/g, list[i].price);
                tr = tr.replace(/#{createTime}/g, list[i].createdTime);
                tr = tr.replace(/#{id}/g, list[i].id);

                $("#product-list").append(tr);
            }
        }
    });
}

/**页面一刷新就显示*/
$(function () {
    $(document).ready(function() {
        showCartList();
    });
})

/**删除商品信息*/
function deleteProduct(id) {
    $.ajax({
        url: "/api/products/"+ id+ "/deleteByid",
        type: "POST",
        xhrFields: {
            withCredentials: true
        },
        dataType: "JSON",
        success: function(json) {
            if (json.state == 200) {
                alert("删除成功");
                showCartList();
            } else {
                alert("操作输错" + json.message);
            }
        },
        error: function(xhr) {
            alert("您的登录信息已经过期，请重新登录！HTTP响应码：" + xhr.status);
            location.href = "login.html";
        }
    });


}


